//
//  UIControl+ActionBlock.m
//  Carpooling
//
//  Created by haidong on 2016/10/27.
//  Copyright © 2016年 MagicBeans. All rights reserved.
//

#import "UIControl+ActionBlock.h"
#import <objc/runtime.h>

static const void *sTouchUoInsideBlockKey = "sTouchUoInsideBlockKey";

@implementation UIControl (ActionBlock)

- (touchUpInsideBlock)touchUpInsideBlock {
    return objc_getAssociatedObject(self, sTouchUoInsideBlockKey);
}

- (void)setTouchUpInsideBlock:(touchUpInsideBlock)touchUpInsideBlock {
    objc_setAssociatedObject(self, sTouchUoInsideBlockKey, touchUpInsideBlock, OBJC_ASSOCIATION_COPY);
    [self removeTarget:self action:@selector(touchUpInsideMethod:) forControlEvents:UIControlEventTouchUpInside];
    if (touchUpInsideBlock) {
        [self addTarget:self action:@selector(touchUpInsideMethod:) forControlEvents:UIControlEventTouchUpInside];
    }
}

- (void)touchUpInsideMethod:(UIButton *)sender {
    touchUpInsideBlock touchUpInside = self.touchUpInsideBlock;
    if (touchUpInside) {
        touchUpInside(sender);
    }
}

@end
